﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="tablecloth/tablecloth.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="tablecloth/tablecloth.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>



<?php
//Meine Spiele


 ini_set( 'display_errors', false );
 // error_reporting( E_ALL );

include("inc/joomlaInt.php");
include("inc/inc_configuration.php");
include("inc/inc_forderungen.php");
include("inc/inc_abwesenheit.php");
include("inc/inc_status.php");

$t1Id = 0;

if ($jUser==0)
{
	echo "Bitte zuerst einloggen!";
}
else
{


	$cQ = "	SELECT 
				lt.teamId,
				t.name,
				lt.leagueId, 
				l.name AS lname,
				l.type
			FROM 
				$tbl_combinations c, 
				$tbl_teams t, 
				$tbl_leagues l, 
				$tbl_lt lt
			WHERE 
				c.userId=$jUser AND 
				c.active=1 AND 
				c.teamId=t.id AND 
				lt.leagueId=l.id AND
				lt.teamId=t.id ";
			
	//echo $cQ;
	$cQe = mysql_query($cQ);


	$canz = mySQL_num_rows($cQe);

	if ($canz>0)
	{
		for ($ic = 0; $ic < $canz; $ic++)
		{
			$t1IdO = mysql_fetch_object($cQe);
			$t1Id = $t1IdO->teamId;
			$l1n = $t1IdO->lname;
			$l1id = $t1IdO->leagueId;
			$type = $t1IdO->type;
			
			if ($type==1)
			{
				$matches=	"SELECT m1.id AS mId, 
									m1.matchday AS spieltag, 
									m1.homeT AS hTi, 
									m1.guestT AS gTi, 
									t1.name AS hTn, 
									t2.name AS gTn,
									t1.id AS hId,
									t2.id AS gId,
									m1.free AS free
									
								FROM $tbl_combinations c1,  $tbl_matches m1, $tbl_teams t1, $tbl_teams t2
								
								WHERE
								((m1.free=1) OR (m1.free=2)) AND
								(m1.leagueId=$l1id) AND
								((c1.userId=$jUser AND
								c1.active=1 AND 
								m1.homeT=c1.teamId AND
								m1.homeT=$t1Id AND
								c1.teamId=t1.id AND
								m1.guestT=t2.id)
								OR
								(c1.userId=$jUser AND
								c1.active=1 AND 
								m1.guestT=c1.teamId AND 
								m1.guestT=$t1Id AND
								c1.teamId=t2.id AND
								m1.homeT=t1.id))
								ORDER BY spieltag
							";
			}
			else
			{
				$matches=	"SELECT m1.id AS mId, 
									m1.matchday AS spieltag, 
									m1.homeT AS hTi, 
									m1.guestT AS gTi, 
									t1.name AS hTn, 
									t2.name AS gTn,
									t1.id AS hId,
									t2.id AS gId,
									m1.free AS free
									
								FROM $tbl_combinations c1,  $tbl_matches m1, $tbl_teams t1, $tbl_teams t2
								
								WHERE
								((m1.free=1) OR (m1.free=2)) AND
								(m1.leagueId=$l1id) AND
								((c1.userId=$jUser AND
								c1.active=1 AND 
								m1.homeT=c1.teamId AND
								m1.homeT=$t1Id AND
								c1.teamId=t1.id AND
								m1.guestT=t2.id)
								OR
								(c1.userId=$jUser AND
								c1.active=1 AND 
								m1.guestT=c1.teamId AND 
								m1.guestT=$t1Id AND
								c1.teamId=t2.id AND
								m1.homeT=t1.id))
								ORDER BY spieltag
							";
			}
						

			$mErgebnis = mysql_query($matches);
			
			$q="SELECT lp.path AS path FROM $tbl_leagues l, $tbl_leaguepics lp WHERE l.id=$l1id AND l.picId=lp.id";
			$qe=mysql_query($q);
			$qeo=mysql_fetch_object($qe);
			$path=$qeo->path;

			echo "<center><img src='img/leagues/",$path,"' width='",$leaguelogowidth,"' height='",$leaguelogoheight,"'></center>"; //alt='$l1n'></center>";
			//echo "<h1>$leaguelogoheight</h1>";
			echo "<table class='middle'>";//border='1px' cellpadding='5'>";
			while ($row = mysql_fetch_object($mErgebnis))
			{
				$tHi=$row->hTi;
				$tGi=$row->gTi;
				
				
				
				$uHSql="	SELECT username AS uHn, c.id AS hC, u.id AS uHi FROM $tbl_combinations c, $tbl_users u
						WHERE
						(c.teamId=$tHi) AND
						(c.active=1) AND
						(c.userId=u.id)";
				$uGSql="	SELECT username AS uGn, c.id AS gC, u.id AS uGi FROM $tbl_combinations c, $tbl_users u
						WHERE
						(c.teamId=$tGi) AND
						(c.active=1) AND
						(c.userId=u.id)";
						
				$uHq = mysql_query($uHSql);
				$uGq = mysql_query($uGSql);

				
				$cH = mySQL_num_rows($uHq);
				$cG = mySQL_num_rows($uGq);
				
				$contactH="";
				$contactG="";
				
				if ($cH == 1)
				{
					$uHnq = mysql_fetch_object($uHq);
					$uHn = $uHnq->uHn;
					$uHi = $uHnq->uHi;
					
					$uHL="<a href='profil.php?id=$uHi'>$uHn</a>";
					
					if ($jUser!=$uHi)
					{
						$contactH=prepareContact($uHi);
					}
					
				}
				else
				{
					$uHL = "(aktuell ohne Trainer)";
				}
				
				if ($cG == 1)
				{
					$uGnq = mysql_fetch_object($uGq);
					$uGn = $uGnq->uGn;
					$uGi = $uGnq->uGi;
					
					$uGL="<a href='profil.php?id=$uGi'>$uGn</a>";
					
					if ($jUser!=$uGi)
					{
						$contactG=prepareContact($uGi);
					}
					
				}
				else
				{
					$uGL = "(aktuell ohne Trainer)";
				}
				
				
				
				
				echo "	<form action='spiel_eintragen.php' method='post'>
						<input type='hidden' value='",$row->mId,"' name='matchId'>
						<input type='hidden' value='",$row->hTn,"' name='hTn'>
						<input type='hidden' value='",$row->gTn,"' name='gTn'>
						<input type='hidden' value='$tHi' name='hTi'>
						<input type='hidden' value='$tGi' name='gTi'>
						<input type='hidden' value='$uHn' name='uHn'>
						<input type='hidden' value='$uGn' name='uGn'>";
				echo "<tr>";		
				echo "<th colspan='5'>",$row->spieltag,". Spieltag</th></tr><tr>";
				echo "	<td>$uHL<br><a href='spiele_team.php?team=$tHi&liga=$l1id'>",$row->hTn,"</a>$contactH
						<td nowrap><a href='team_kader.php?team=$tHi'><img src='img/logos/",$row->hId,".png' width='35' height='35' alt=''></a> <b>:</b> 
						<a href='team_kader.php?team=$tGi'><img src='img/logos/",$row->gId,".png' width='35' height='35' alt=''></a></td>
						";
				echo "	<td>$uGL
						<br><a href='spiele_team.php?team=$tGi&liga=$l1id'>",$row->gTn,"</a>
						$contactG</td>";
				echo "<td class='middle'>";	
				if (($cG == 1) AND ($cH == 1))
				{
					$free=$row->free;
					if ($free==2)
					{
						echo "Warte auf Bestätigung.";
					}
					else
					{
						echo "<input type='submit' name='submit' class='button eintragen' value='Ergebnis eintragen'>
										</form>
										
										<form action='forderungen.php?matchId=",$row->mId,"' method='post'>
											<input type='submit' name='submit' class='button fordern' value='Gegner fordern'>
											
											

											
										</form>";
					}
				}					
				else
				{
					echo "Kein Fordern oder <br>Eintragen möglich";
				}

				echo "</td></tr>";
				
				$mId=$row->mId;
				
				//FORDERUNGEN
				
				$f=get_forderung_verfuegbar($jUser,$mId);
				
				echo"<tr><td colspan='5'>$f</td></tr>";
			}
			echo"</table>";
		}
			
	}


	//Internationale Spiele


	$cQ= "	SELECT 
				t.id AS teamId,
				t.name,
				l.name AS lname,
				l.type,
				l.id AS leagueId,
				lg.groupName AS gName,
				lg.id AS gId
			FROM 
				$tbl_combinations c, 
				$tbl_teams t, 
				$tbl_leagues l,
				$tbl_gt gt,
				$tbl_lg lg
			WHERE 			
				c.userId=$jUser AND 
				c.active=1 AND 
				c.teamId=t.id AND 
				lg.leagueId=l.id AND
				gt.teamId=t.id AND
				gt.groupId=lg.id";
				
	$cQe = mysql_query($cQ);


	$canz = mySQL_num_rows($cQe);
	$cQe = mysql_query($cQ);

	$canz = mySQL_num_rows($cQe);

	if ($canz>0)
	{
		for ($ic = 0; $ic < $canz; $ic++)
		{
			$t1IdO = mysql_fetch_object($cQe);
			$t1Id = $t1IdO->teamId;
			$l1n = $t1IdO->lname;
			$l1id = $t1IdO->leagueId;
			$type = $t1IdO->type;
			$gName = $t1IdO->gName;
			$gId = $t1IdO->gId;
			
			
			$matches=	"SELECT m1.id AS mId, 
								m1.matchday AS spieltag, 
								m1.homeT AS hTi, 
								m1.guestT AS gTi, 
								t1.name AS hTn, 
								t2.name AS gTn,
								t1.id AS hId,
								t2.id AS gId,
								m1.free AS free
								
							FROM $tbl_combinations c1,  $tbl_matches m1, $tbl_teams t1, $tbl_teams t2
							
							WHERE
							((m1.free=1) OR (m1.free=2)) AND
							(m1.leagueId=$l1id) AND
							((c1.userId=$jUser AND
							c1.active=1 AND 
							m1.homeT=c1.teamId AND
							m1.homeT=$t1Id AND
							c1.teamId=t1.id AND
							m1.stageId=$gId AND
							m1.guestT=t2.id)
							OR
							(c1.userId=$jUser AND
							c1.active=1 AND 
							m1.guestT=c1.teamId AND 
							m1.stageId=$gId AND
							m1.guestT=$t1Id AND
							c1.teamId=t2.id AND
							m1.homeT=t1.id))
							ORDER BY spieltag
						";
						

			$mErgebnis = mysql_query($matches);
			
			$mc=mysql_num_rows($mErgebnis);
			
			if ($mc>0)
			{
				$q="SELECT lp.path AS path FROM $tbl_leagues l, $tbl_leaguepics lp WHERE l.id=$l1id AND l.picId=lp.id";
				$qe=mysql_query($q);
				$qeo=mysql_fetch_object($qe);
				$path=$qeo->path;

				echo "<center><img src='img/leagues/",$path,"' width='",$leaguelogowidth,"' height='",$leaguelogoheight,"' alt='$l1n'></center>";
				echo "<h1>$gName</h1>";
				echo "<table class='middle'> ";//border='1px' cellpadding='5'>";
				while ($row = mysql_fetch_object($mErgebnis))
				{
					$tHi=$row->hTi;
					$tGi=$row->gTi;
					
					
					$uHSql="	SELECT username AS uHn, c.id AS hC, u.id AS uHi FROM $tbl_combinations c, $tbl_users u
						WHERE
						(c.teamId=$tHi) AND
						(c.active=1) AND
						(c.userId=u.id)";
				$uGSql="	SELECT username AS uGn, c.id AS gC, u.id AS uGi FROM $tbl_combinations c, $tbl_users u
						WHERE
						(c.teamId=$tGi) AND
						(c.active=1) AND
						(c.userId=u.id)";
						
				$uHq = mysql_query($uHSql);
				$uGq = mysql_query($uGSql);

				
				$cH = mySQL_num_rows($uHq);
				$cG = mySQL_num_rows($uGq);
				
				$uHi=0;
				$uGi=0;
				$contactH="";
				$contactG="";
				
				if ($cH == 1)
				{
					$uHnq = mysql_fetch_object($uHq);
					$uHn = $uHnq->uHn;
					$uHi = $uHnq->uHi;
					
					$uHL="<a href='profil.php?id=$uHi'>$uHn</a>";
					
					if ($jUser!=$uHi)
					{
						$contactH=prepareContact($uHi);
					}
					
				}
				else
				{
					$uHL = "(aktuell ohne Trainer)";
				}
				
				if ($cG == 1)
				{
					$uGnq = mysql_fetch_object($uGq);
					$uGn = $uGnq->uGn;
					$uGi = $uGnq->uGi;
					
					$uGL="<a href='profil.php?id=$uGi'>$uGn</a>";
					
					if ($jUser!=$uGi)
					{
						$contactG=prepareContact($uGi);
					}
					
				}
				else
				{
					$uGL = "(aktuell ohne Trainer)";
				}
				
				
					
					
					echo "	<form action='spiel_eintragen.php' method='post'>
							<input type='hidden' value='",$row->mId,"' name='matchId'>
							<input type='hidden' value='",$row->hTn,"' name='hTn'>
							<input type='hidden' value='",$row->gTn,"' name='gTn'>
							<input type='hidden' value='$tHi' name='hTi'>
							<input type='hidden' value='$tGi' name='gTi'>
							<input type='hidden' value='$uHn' name='uHn'>
							<input type='hidden' value='$uGn' name='uGn'>";
					echo "<tr>";		
					echo "<th colspan='5'>",$row->spieltag,". Spieltag</th></tr><tr>";//<td>",$row->mId,"</td>";
					echo "	<td>$uHL<br><a href='spiele_team.php?team=$tHi&liga=$l1id'>",$row->hTn,"</a>$contactH
							<td nowrap><a href='team_kader.php?team=$tHi'><img src='img/logos/",$row->hId,".png' width='35' height='35' alt=''></a> <b>:</b> 
							<a href='team_kader.php?team=$tGi'><img src='img/logos/",$row->gId,".png' width='35' height='35' alt=''></a></td>
							";
					echo "	<td>$uGL
							<br><a href='spiele_team.php?team=$tGi&liga=$l1id'>",$row->gTn,"</a>
							$contactG</td>";
					echo "<td class='middle'>";	
					if (($cG == 1) AND ($cH == 1))
					{
						$free=$row->free;
						if ($free==2)
						{
							echo "Warte auf Bestätigung.";
						}
						else
						{
							echo "<input type='submit' name='submit' class='button eintragen' value='Ergebnis eintragen'>
											</form>
											
											<form action='forderungen.php?matchId=",$row->mId,"' method='post'>
												<input type='submit' name='submit' class='button fordern' value='Gegner fordern'>
												
												

												
											</form>";
						}
					}					
					else
					{
						echo "Kein Fordern oder <br>Eintragen möglich";
					}

					echo "</td></tr>";
					
					$mId=$row->mId;
					
					//FORDERUNGEN
					
					$f=get_forderung_verfuegbar($jUser,$mId);
					
					echo"<tr><td colspan='5'>$f</td></tr>";
				}
				echo"</table><br><br>";
			}
		}
			
	}
		


	echo '<center><br><br><a href="javascript:window.history.back()" class="button zurueck">Zurück</a></center>';
}
?>

</body>
</html>